<template>
  <dialog-View :isShow="_isOpen" :title="'添加备注信息'" :width="'30%'">
    <form-View
      :formList="formList"
      :rules="rules"
      :position="'left'"
      :editItem="orderDetail"
      @update="handelSubmit"
      @back="handelCancel"
    ></form-View>
  </dialog-View>
</template>
<script setup>
import dialogView from "../../components/dialogView.vue";
import formView from "../../components/formView.vue";
import { closeOrder, updateOrderInfo } from "@/apis/order";
import { computed } from "vue";

const formList = [
  {
    label: "请选择关闭原因",
    model: "closeReason",
    type: "select",
    placeholder: "请选择关闭原因",
    data: [
      { label: "联系不上商家", value: 1 },
      { label: "买家不喜欢不想要", value: 2 },
      { label: "缺货无法交易", value: 3 }
    ]
  }
];
const rules = {
  closeReason: [{ required: true, message: "请选择关闭原因", trigger: "blur" }]
};

const props = defineProps({
  modelValue: { type: Boolean, default: false },
  orderDetail: { type: Object, default: () => ({}) }
});

const emit = defineEmits(["update:modelValue", "success"]);

const _isOpen = computed({
  get: () => props.modelValue,
  set: newVal => emit("update:modelValue", newVal)
});

const handelSubmit = async ({ closeReason }) => {
  await closeOrder({
    isClose: 1,
    orderId: props.orderDetail.orderId,
    closeReason: closeReason === 1 ? "联系不上商家" : closeReason === 2 ? "买家不喜欢不想要" : "缺货无法交易"
  });

  _isOpen.value = false;
  emit("success", "关闭订单成功");
};

const handelCancel = () => {
  _isOpen.value = false;
};
</script>
